﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using GameAutomationLibrary;

namespace MyBrowser2008
{
    public partial class Form1 : Form
    {
        public double InputZone
        {
            get { return Convert.ToDouble(tbtUrl.Text); }
            set { tbtUrl.Text = value.ToString(); }
        }
        CookieContainer myCookieContainer = new CookieContainer();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axWebBrowser1.BeforeNavigate2 += axWebBrowser1_BeforeNavigate2;
            axWebBrowser1.NavigateComplete2 += axWebBrowser1_NavigateComplete2;

        }


        private void btnRefresh_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(tbtUrl.Text);
            webBrowser2.Navigate("http://astd.37wan.com/game_login.php?server=S85");
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            btnLogin.Enabled = true;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbAccount.Text) || string.IsNullOrEmpty(tbPassword.Text))
            {
                MessageBox.Show("输入帐号密码");
                return;
            }
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement htmlUid = doc.GetElementById("email");//用户名文本框ID
            htmlUid.SetAttribute("value", tbAccount.Text);//你要填充的用户名
            HtmlElement htmlPwd = doc.GetElementById("password");//密码文本框ID
            htmlPwd.SetAttribute("value", tbPassword.Text);//你要填充的密码文本
            HtmlElement htmlSubmitButton = doc.GetElementById("login");//提交按钮ID
            htmlSubmitButton.InvokeMember("Click");

        }
        private void btnLoginAstd_Click(object sender, EventArgs e)
        {
            HtmlDocument doc = webBrowser2.Document;
            HtmlElement htmlUid = doc.GetElementById("username");//用户名文本框ID
            htmlUid.SetAttribute("value", tbAccount.Text );//你要填充的用户名
            HtmlElement htmlPwd = doc.GetElementById("password");//密码文本框ID
            htmlPwd.SetAttribute("value", tbPassword.Text );//你要填充的密码文本
            
            //HtmlElement htmlSubmitButton = doc.GetElementById("login");//提交按钮ID
            //htmlSubmitButton.InvokeMember("Click");

            doc.InvokeScript("login");

            #region MyRegion

            /*
    
            string urlBase =
                "http://astd.37wan.com/login.php?action=login&login_account={0}&password={1}&remember_me=undefined";
            string url = string.Format(urlBase, "hbzhuxuz", "123456");
            CookieContainer cookies = new CookieContainer();
            string result = HttpRequestHandler.HttpGet(url, cookies);


            Console.WriteLine(result);
            Console.WriteLine("--------------------------------------------------");

            url = "http://astd.37wan.com/entergame.php?server=S62";
            result = HttpRequestHandler.HttpGet(url, cookies);

            
            url = "http://s62.astd.37wan.com";
            string cookiesString = cookies.GetCookieHeader(new Uri(url));
            string[] strarry1 = cookiesString.Split(';');
            string var0 = string.Empty;
            string var1 = string.Empty;
            string var2 = string.Empty;
            string var3 = string.Empty;
            foreach (string s in strarry1)
            {
                if (!s.StartsWith("passport_37wan_com="))
                    continue;
                string ns = s.Replace("passport_37wan_com=", "");
                ns = ns.Replace("%7C", ";");
                string[] strarry2 = ns.Split(';');
                var0 = strarry2[0];
                var1 = strarry2[1];
                var2 = strarry2[2];
                var3 = strarry2[3];

            }

            string realUrl =
                string.Format(
                    "http://s62.astd.37wan.com/root/start.action?accid={0}&accname={1}&serverid={2}&tstamp={3}&fcm=1&ticket={4}", var0, var1, "62", var2, var3);
            //PHPSESSID=425d5779f791f7e5b74d5184b52a4e75; passport_37wan_com=28213608%7Chbzhuxuz%7C1297518226000%7Ce87661c312adf02f412c23270148ef4e; ispass_37wan_com=69c5fceb%7C-1%7C036e7284c90b27cf911695326601e297
            //http://s62.astd.37wan.com/root/start.action?accid=28213608&accname=hbzhuxuz&serverid=62&tstamp=1297520742&fcm=1&ticket=762fc4409164309303f48f87eae72396
            //webBrowser2.Headers.Add("Cookie", "PHPSESSID=" + cookie + ";");
            webBrowser2.Navigate(realUrl);

            Console.WriteLine(result);
            Console.WriteLine("--------------------------------------------------");

             
             */

            #endregion

        }

        private void btnLogin3_Click(object sender, EventArgs e)
        {
            axWebBrowser1.Navigate("http://astd.37wan.com/game_login.php?server=S85");
            axWebBrowser1.DocumentComplete += axWebBrowser1_DocumentComplete;
        }

        void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            axWebBrowser1.DocumentComplete -= axWebBrowser1_DocumentComplete;
            //axWebBrowser1.Navigate("http://astd.37wan.com/login.php?action=loginGame");
            axWebBrowser1.Navigate("http://astd.37wan.com/login.php?action=loginGame&login_account=hbzhuxuz&password=123456");

        }
        void axWebBrowser1_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
        {

            Console.WriteLine("Start with header:====\r\n" + e.headers.ToString() + "\r\n============\r\n\r\n");
            //throw new NotImplementedException();
        }
        void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            Console.WriteLine("End with header:----\r\n" + e.uRL.ToString() + "\r\n-------------\r\n\r\n");
        }

    }
}
